<template>
  <div class="login-container">
    <div v-if="!isLogin()">
      <!-- 表格 -->
      <el-form
        ref="form"
        :model="form"
        class="login-form"
        label-width="70px"
        :rules="rules"
      >
        <h2 class="login-title">登录</h2>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>

        <div class="button">
          <el-button
            type="primary"
            @click="submitForm('form')"
            class="login-button"
            >登录</el-button
          >
        </div>
        <div class="change">
          点击&nbsp;
          <router-link to="/register">这里</router-link> &nbsp;注册!
        </div>
        <!-- <div class="notification is-danger" v-if="errors.length">
          <div
            v-for="error in errors"
            v-bind:key="error"
            style="width: 100%"
            class="sign"
          >
            {{ error }}
          </div>
        </div> -->
      </el-form>
    </div>
    <div v-else class="success">
      <el-button type="danger" @click="signOut" class="signout-button"
        >登出</el-button
      >
    </div>
  </div>
</template>

<script>
  import { h } from "vue";
  import axios from "axios";
  import { ElButton, ElForm, ElInput, ElFormItem } from "element-plus";

  export default {
    name: "LogIn",
    data() {
      return {
        form: {
          username: "",
          password: "",
        },
        rules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "change" },
            {
              min: 1,
              max: 15,
              message: "长度在 1 到 15 个字符",
              trigger: "blur",
            },
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "change" },
          ],
        },
      };
    },
    components: {
      ElButton,
      ElForm,
      ElInput,
      ElFormItem,
    },
    mounted() {
      this.gotoMeCreate();
      document.title = "Log In";
    },
    methods: {
      gotoMeCreate: function () {
        if (this.$options.methods.isLogin() === true) {
          this.$router.push("/meCreate");
        }
      },
      async submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.errors = [];

            if (!this.errors.length) {
              axios.post("login/", this.form).then((response) => {
                if (response.data.status == 403) {
                  this.$notify.error({
                    title: "失败",
                    message: "用户不存在！",
                  });
                } else if (response.data.status == 401) {
                  this.$notify.error({
                    title: "失败",
                    message: "密码错误！",
                  });
                } else {
                  localStorage.setItem("token", response.data.token);
                  this.$router.push("/meCreate");
                  localStorage.setItem("username", this.form.username);
                  this.$notify({
                    title: "成功",
                    message: h(
                      "i",
                      { style: "color: green; font-family: '微软雅黑'" },
                      "登录成功！"
                    ),
                  });
                }
              });
            }
          }
        });
      },
      signOut() {
        localStorage.setItem("token", "");
        location.reload();
      },
      isLogin() {
        if (localStorage.getItem("token")) {
          return true;
        }
        return false;
      },
    },
  };
</script>

<style scoped>
.login-form {
  width: 350px;
  margin: 160px auto; /* 上下间距160px，左右自动居中*/
  background-color: rgb(255, 255, 255, 0.8); /* 透明背景色 */
  padding: 30px;
  border-radius: 20px; /* 圆角 */
}

/* 背景 */
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  font-family: "微软雅黑";
  /* background: url("../assets/home.jpg"); */
  background-image: url(../assets/home.jpg);
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}

/* 标题 */
.login-title {
  color: #303133;
  text-align: center;
}
.success {
  height: 100%;
  display: flex;
  justify-content: center;
  margin: 0 auto;
  align-items: center;
}

.signout-button {
  height: 20px;
}

.change,
.notification,
.button {
  display: flex;
  align-items: center;
  justify-content: center;
}

.el-button {
  width: 100px;
}
</style>